<?
//*********************************************************************//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Edit form ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// This page relies on the TORRENT_FORM class. All it does is call	 //
// the necessary functions.											//
//---------------------------------------------------------------------//
// At the bottom, there are grouping functions which are off limits to //
// most members.													   //
//*********************************************************************//

require(SERVER_ROOT.'/classes/class_torrent_form.php');

if(!is_number($_GET['id']) || !$_GET['id']) { error(0); }

$TorrentID = $_GET['id'];

$DB->query("SELECT 
	t.Media, 
	t.Format, 
	t.Encoding AS Bitrate, 
	t.RemasterYear, 
	t.Remastered, 
	t.RemasterTitle, 
	t.RemasterCatalogueNumber,
	t.RemasterRecordLabel,
	t.Scene, 
	t.FreeTorrent, 
	t.Dupable, 
	t.DupeReason, 
	t.Description AS TorrentDescription, 
	tg.CategoryID,
	tg.Name AS Title,
	tg.Year,
	tg.ArtistID,
	ag.Name AS ArtistName,
	t.GroupID,
	t.UserID,
	t.HasLog,
	t.HasCue,
	t.LogScore,
	s.AdjustmentReason
	FROM torrents AS t 
	JOIN torrents_group AS tg ON tg.ID=t.GroupID
	LEFT JOIN torrents_logs_new AS s ON s.TorrentID=t.ID
	LEFT JOIN artists_group AS ag ON ag.ArtistID=tg.ArtistID
	WHERE t.ID='$TorrentID'");

list($Properties) = $DB->to_array(false,MYSQLI_BOTH);
$UploadForm = $Categories[$Properties['CategoryID']-1];

if($LoggedUser['Err']) {
	$Err = $LoggedUser['Err'];
	unset($_SESSION['logged_user']['Err']);
}

show_header('Edit torrent', 'upload');
show_message();
if(!$Properties) { error(404); }

if(($LoggedUser['ID']!=$Properties['UserID'] && !check_perms('torrents_edit')) || $LoggedUser['DisableWiki']) {
	error(403);
}

if(!($Properties['Remastered'] && !$Properties['RemasterYear'] && $TorrentID > 1309521) || check_perms('edit_unknowns')) {
	$TorentForm = new TORRENT_FORM($Properties, $Err, false);
	
	$TorentForm->head();
	switch ($UploadForm) {
		case 'Music':
			$TorentForm->music_form('');
			break;
			
		case 'Audiobooks':
		case 'Comedy':
			$TorentForm->audiobook_form();
			break;
		
		case 'Applications':
		case 'Comics':
		case 'E-Books':
		case 'E-Learning Videos':
			$TorentForm->simple_form();
			break;
		default:
			$TorentForm->music_form('');
	}
	$TorentForm->foot();
}

if(check_perms('torrents_edit') && $Properties['CategoryID'] == 1){
?>
<div class="thin">
	<h2>Change Group</h2>
	<form action="torrents.php" method="post">
		<table>
			<tr>
				<td class="label">Group ID</td>
				<td>
						<input type="hidden" name="action" value="editgroupid" />
						<input type="hidden" name="torrentid" value="<?=$TorrentID?>" />
						<input type="hidden" name="oldgroupid" value="<?=$Properties['GroupID']?>" />
						<input type="text" name="groupid" value="<?=$Properties['GroupID']?>" size="10" />
				</td>
			</tr>
			<tr>
				<td colspan="2" class="center">
						<input type="submit" value="Change group ID" />
				</td>
			</tr>
		</table>
	</form>
	<h2>Split off into new group</h2>
	<form action="torrents.php" method="post">
		<table>
			<tr>
				<td class="label">Artist</td>
				<td>
						<input type="hidden" name="action" value="newgroup" />
						<input type="hidden" name="torrentid" value="<?=$TorrentID?>" />
						<input type="hidden" name="oldgroupid" value="<?=$Properties['GroupID']?>" />
						<input type="hidden" name="oldartistid" value="<?=$Properties['ArtistID']?>" />
						<input type="text" name="artist" value="<?=$Properties['ArtistName']?>" size="50" />
				</td>
			</tr>
			<tr>
				<td class="label">Title</td>
				<td>
						<input type="text" name="title" value="<?=$Properties['Title']?>" size="50" />
				</td>
			</tr>
			<tr>
				<td class="label">Year</td>
				<td>
						<input type="text" name="year" value="<?=$Properties['Year']?>" size="10" />
				</td>
			</tr>
			<tr>
				<td colspan="2" class="center">
						<input type="submit" value="Split into new group" />
				</td>
			</tr>
		</table>
	</form>
		
	<br />
</div>
<?
} // if check_perms('torrents_edit')

show_footer();
?>
